$(document).ready(function() {

	// -------------------------------------------- PERSON REGISTERS -----------------------------------------
	$("#reg_button").live('click',function(){
		$("#reg_success").slideUp();
		var username = $("#reg_username").val();
		var firstname = $("#reg_firstname").val();
		var lastname = $("#reg_lastname").val();
		var email = $("#reg_email").val();
		var contactnumber = $("#reg_contactnumber").val();
		if(username == '' || firstname=='' || lastname=='' || email=='' || contactnumber=='')
		{
			$("#reg_error_exist").slideUp();
			$("#reg_error_email").slideUp();
			$("#reg_error").slideDown();
		}
		else
		{
			$.post("../User/validateInformation",{
				"email": email,
				"contactnumber": contactnumber
			},function(data){
				if(data.toString() == "False")
				{
					$("#reg_error_exist").slideUp();
					$("#reg_error").slideUp();
					$("#reg_error_email").slideDown();
				}
				else
				{	
					$.post("../User/Register",{
					"username": username,
					"firstname": firstname,
					"lastname": lastname,
					"email": email,
					"contactnumber" : contactnumber
					},function(data){
						if(data.toString() == "False")
						{
							$("#reg_error_email").slideUp();
							$("#reg_error").slideUp();
							$("#reg_error_exist").slideDown();
						}
						else
						{							
							$(".error").slideUp();
							$("#reg_success").slideDown();							    
							$("#reg_username").val("");
							$("#reg_firstname").val("");
							$("#reg_lastname").val("");
							$("#reg_email").val("");
							$("#reg_contactnumber").val("");
						}						   
					});	
				}						   
			});					
		}
	});
	
	// -------------------------------------------- USER CANCELS REGISTRATION -----------------------------------------		
	$("#canc_button").live('click',function(){
		window.location.replace("../Home");
		
		return false;
	});
	
	// ----------------------------------------- SEARCH FOR A USER ----------------------------------------
	$("#s_user").live('keyup',function(){
		var search = $("#s_user").val();
		$.post("../User/SearchUser",{
		    "search": search
			},function(data){
				var content = "";
				$.each(data, function(i, users){
				content += "<tr id=\""+users.user_ID+"_row\" class=\"user_row\">";
				content += "<td align=\"center\" class=\"user_column\" id=\""+users.username+"_username\">"+users.username+"</td>";
				content += "<td align=\"center\" class=\"user_column\" id=\""+users.username+"_firstname\">"+users.firstname+"</td>";
				content += "<td align=\"center\" class=\"user_column\" id=\""+users.username+"_lastname\">"+users.lastname+"</td>";
				if (users.level==1)
					var level = "User";
				else if (users.level==2)
					var level = "Librarian";
				else
					var level = "Admin";
				content += "<td align=\"center\" class=\"user_column\" id=\""+users.username+"_level\">"+level+"</td>";
				content += "<td align=\"center\"><a class=\"editUser_showForm\" id=\""+users.username+"\"  href=\"#\">edit</a>&nbsp;|&nbsp;<a class=\"deleteUser\" id=\""+users.username+"\" href=\"#\">delete</a>";
				content += "<input name=\""+users.username+"_userid\" id=\""+users.username+"_userid\" type=\"hidden\" value=\""+users.user_ID+"\" />";
				content += "<input name=\""+users.username+"_email\" id=\""+users.username+"_email\" type=\"hidden\" value=\""+users.email+"\" />";
				content += "<input name=\""+users.username+"_contactnumber\" id=\""+users.username+"_contactnumber\" type=\"hidden\" value=\""+users.contactnumber+"\" /></td>";
				content += "</tr>";
									
			});
			$("#bt_user").html(content);
		}, "JSON");
	});
		
	// -------------------------------------------- USER CANCELS EDITING OF PASSWORD -----------------------------------------		
	$("#cancelEditPassword").click(function(){
		$("#changePassword").slideUp();
		$("#normalAccount").slideDown();
		
		return false;
	});
		
	// -------------------------------------------- USER EDITS PASSWORD -----------------------------------------		
	$("#editPassword").click(function(){
		var oldPassword = $("#oldPassword").val();
		var newPassword = $("#newPassword").val();
		var confirm = $("#confirmNewPassword").val();
		if (newPassword != confirm)
		{
			$("#passconfirm_error").slideDown();
			$("#pass_error").slideUp();			
		}
		else
		{
			$.post("../User/changePassword",{
					"oldPassword": oldPassword,
					"newPassword": newPassword
			},function(data){
				if(data.toString() == "False")
				{
					$("#pass_error").slideDown();
					$("#passconfirm_error").slideUp();
				}
				else
				{
					$("#passconfirm_error").slideUp();
					$("#pass_error").slideUp();
					$("#changePassword").slideUp();
					$("#oldPassword").val("");	
					$("#newPassword").val("");	
					$("#confirmNewPassword").val("");	
					$("#pass_success").slideDown(800).delay(800).slideUp();
					$("#normalAccount").slideDown();
				}	    
			});
		}
		
		return false;
	});
		
	// -------------------------------------------- USER WANTS TO EDIT INFO -----------------------------------------		
	$("#editAccount").click(function(){
		$("#editusername").attr("disabled", false);
		$("#editfirstname").attr("disabled", false);
		$("#editlastname").attr("disabled", false);
		$("#editemail").attr("disabled", false);
		$("#editcontact").attr("disabled", false);
		$("#normalAccount").slideUp();	
		$("#editAccounts").slideDown();	
		
		return false;
	});
		
	// -------------------------------------------- USER CANCELS EDITING OF INFO -----------------------------------------		
	$("#cancelEditAccount").click(function(){
		$.post("../User/getUser",{
		},function(data){							    
			$("#editfirstname").val(data.firstname);
			$("#editlastname").val(data.lastname);
			$("#editusername").val(data.username);
			$("#editemail").val(data.email);
			$("#editcontact").val(data.contactnumber);	
			$("#editusername").attr("disabled", true);
			$("#editfirstname").attr("disabled", true);
			$("#editlastname").attr("disabled", true);
			$("#editemail").attr("disabled", true);
			$("#editcontact").attr("disabled", true);
			$("#normalAccount").slideDown();	
			$("#editAccounts").slideUp();
			
			return false;
					    
		}, "json");
	});
		
	// -------------------------------------------- USER EDITS INFO -----------------------------------------		
	$("#editAccountFinal").click(function(){
		var username = $("#editusername").val();
		var firstname = $("#editfirstname").val();
		var lastname = $("#editlastname").val();
		var email = $("#editemail").val();
		var contactnumber = $("#editcontact").val();
		
		if(username == '' || firstname=='' || lastname=='' || email=='' || contactnumber=='')
		{
			$("#accounterror").slideUp();
			$("#accountemail").slideUp();
			$("#accounterror2").slideDown();
		}
		else
		{
			$.post("../User/validateInformation",{
				"email": email
			},function(data){
				if(data.toString() == "False")
				{
					$("#accounterror").slideUp();
					$("#accounterror2").slideUp();
					$("#accountemail").slideDown();
				}
				else
				{	
					$.post("../User/editAccount",{
					"username": username,
					"firstname": firstname,
					"lastname": lastname,
					"email": email,
					"contact" : contactnumber
					},function(data){
						if(data.toString() == "False")
						{
							$("#accounterror2").slideUp();
							$("#accountemail").slideUp();
							$("#accounterror").slideDown();
						}
						else
						{							
							$(".error").slideUp();
							$("#accountsuccess").slideDown(800).delay(800).slideUp();
							$("#editusername").attr("disabled", true);
							$("#editfirstname").attr("disabled", true);
							$("#editlastname").attr("disabled", true);
							$("#editemail").attr("disabled", true);
							$("#editcontact").attr("disabled", true);
							$("#normalAccount").slideDown();	
							$("#editAccounts").slideUp();
			
						}						   
					});	
				}						   
			});					
		}
		
		return false;
	});
		
	// -------------------------------------------- USER CANCELS PASSWORD CHANGE-----------------------------------------		
	$("#changePass").click(function(){
		$("#normalAccount").slideUp();
		$("#changePassword").slideDown();
		
		return false;
	});
		
	// -------------------------------------------- USER CANCELS CHANGE OF PASSWORD -----------------------------------------		
	$("#confirmChangePass").click(function(){
		var newPassword = $("#password").val();
		var confirm = $("#confirm").val();
		if (newPassword != confirm)
		{
			$("#errorchangepass").slideDown();		
		}
		else if (newPassword=="")
			$("#errorchangepass").slideDown();				
		else
		{
			$.post("../User/changePasswordFirstLogin",{
					"password": newPassword
			},function(data){ 
				console.log(data);
				if (data==1)
					window.location.replace("../User/Index");
				else
					window.location.replace("../Librarian");   
			});
		}
		return false;
	});			
				
	// -------------------------------------------- USER CANCELS RESERVATION -----------------------------------------			
	$(".cancel_reservation").live('click',function(){
		var reservationid = $(this).attr("id");	
		var row = "#"+reservationid+"_row";			
		
		$.post("../User/cancelReservation",{
			"reservation_ID": reservationid
		},function(data){
			$("#cancel_reservation_success").slideDown(800).delay(800).slideUp();
			$(".error").slideUp();
			$(row).fadeOut();
		});
		
		return false;
	});
	
	// -------------------------------------------- ADMIN APPROVES REGISTRATION -----------------------------------------				
	$(".approve_reg").click(function(){
		var signup_ID = $(this).attr("id");
		var realLevel = $("#"+signup_ID+"_level").val();		
		if (realLevel=="User")
			var level = 1;
		else if (realLevel=="Librarian")
			var level = 2;
		else
			var level = 3;					
		var row = "#"+signup_ID+"_row";
		$.post("../Librarian/approveRegistration",{
			"signup_ID": signup_ID,
			"level": level
		},function(data){
			if (data.toString() == "False")
			{
				$("#reg_error").slideDown(800).delay(800).slideUp();
			}
			else
			{
				$("#reg_success").slideDown(800).delay(800).slideUp();
				$(row).fadeOut();
			}
		});
	});
	
	// -------------------------------------------- ADMIN REJECTS REGISTRATION -----------------------------------------				
	$(".reject_reg").click(function(){
		var signup_ID = $(this).attr("id");				
		var row = "#"+signup_ID+"_row";
		$.post("../Librarian/rejectRegistration",{
			"signup_ID": signup_ID
		},function(data){
			$("#reg_successs").slideDown(800).delay(800).slideUp();
			$(row).fadeOut();
		});
		
		return false;
	});
	
	// -------------------------------------------- ADMIN ADDS USERS -----------------------------------------			
	$("#au_button").live('click',function(){
		var username = $("#au_username").val();
		var pass = $("#au_pass").val();
		var confirm = $("#au_confirm").val();
		var fname = $("#au_fname").val();
		var lname = $("#au_lname").val();
		var email = $("#au_email").val();
		var contact = $("#au_contact").val();
		var level = $("#au_level").val();
		var realLevel;
		if (level=="User")
			realLevel = 1;
		else if (level=="Librarian")
			realLevel = 2;
		else
			realLevel = 3;					
		if(username == '' || pass == '' || confirm == '' || fname == '' || lname == '' || email == '' || contact == '')
		{		
			$("#au_error_email").slideUp();
			$("#au_error_pass").slideUp();
			$("#au_error").slideDown();
		}
		else if(pass != confirm)
		{
			$("#au_error_email").slideUp();
			$("#au_error").slideUp();
			$("#au_error_pass").slideDown();
		}
		else
		{
			$.post("../User/validateInformation",{
				"email": email,
				"contactnumber": contact
			},function(data){
				if (data.toString()=="True")
				{
					$.post("../User/AddUser",{
					"username": username,
					"password": pass,
					"firstname": fname,
					"lastname": lname,
					"email": email,
					"contact": contact,
					"level" : realLevel
					},function(data){
						if(data.toString() == "null")
						{
							$("#au_error_exist").slideDown(800).delay(800).slideUp();
						}
						else
						{						
							$("#bt_user").slideDown().append("	<tr id=\""+data.username+"_row\" class=\"user_row\">"+
							"<td align=\"center\" class=\"user_column\" id=\""+data.username+"_username\">"+  data.username +"</td>" +
							"<td align=\"center\" class=\"user_column\" id=\""+data.username+"_firstname\">"+  data.firstname +"</td>" +
							"<td align=\"center\" class=\"user_column\" id=\""+data.username+"_lastname\">"+  data.lastname +"</td>" +
							"<td align=\"center\" class=\"user_column\" id=\""+data.username+"_level\">"+  level +"</td>" +
							"<td align=\"center\"><a class=\"editUser_showForm\" id=\""+data.username+"\"  href=\"#\">edit&nbsp;|&nbsp;<a class=\"deleteUser\" id=\""+data.username+"\" href=\"#\">delete</a></td>" +
							"<input name=\""+data.username+"_userid\" id=\""+data.username+"_userid\" type=\"hidden\" value=\""+data.user_ID+"\" />" +
							"<input name=\""+data.username+"_email\" id=\""+data.username+"_email\" type=\"hidden\" value=\""+data.email+"\" />" +
							"<input name=\""+data.username+"_contactnumber\" id=\""+data.username+"_contactnumber\" type=\"hidden\" value=\""+data.contactnumber+"\" /></td>" +
							"</tr>");																									
							$(".error").slideUp();
							$("#au_success").slideDown(800).delay(800).slideUp();							    
							$("#au_username").val("");
							$("#au_pass").val("");
							$("#au_confirm").val("");
							$("#au_fname").val("");
							$("#au_lname").val("");
							$("#au_email").val("");
							$("#au_contact").val("");
							$("#au_level").val("");
						}						   
					}, "json");	
				}
				else
				{
					$("#au_error").slideUp();
					$("#au_error_pass").slideUp();
					$("#au_error_email").slideDown();					
				}			   
			});
		}
		return false;			
	});				
				
	// -------------------------------------------- ADMIN DELETES USER -----------------------------------------			
	$(".deleteUser").live('click',function(){
		var username = $(this).attr("id");		
		var userid = $("#"+username+"_userid").val();
		var row = "#"+username+"_row";			
					
		$.post("../User/deleteUser",{
			"user_id": userid
		},function(data){
			$("#du_success").slideDown(800).delay(800).slideUp();
			$(".error").slideUp();
			$(row).fadeOut();
		});
		
		return false;
	});
	
	// -------------------------------------------- ADMIN WANTS TO EDIT USER INFO -----------------------------------------			
	$(".editUser_showForm").live('click',function(){
		$("#form_editUser").slideDown();
		var userid = $(this).attr("id"); 
		$("#inp_userid").val( $("#"+userid+"_userid").val() );
		$("#inp_username").val( $("#"+userid+"_username").html() );
		$("#inp_firstname").val( $("#"+userid+"_firstname").html() );
		$("#inp_lastname").val( $("#"+userid+"_lastname").html() );
		$("#inp_email").val( $("#"+userid+"_email").val() );
		$("#inp_level").val( $("#"+userid+"_level").html() );
		$("#inp_contactnumber").val( $("#"+userid+"_contactnumber").val() );
	});
		
	// -------------------------------------------- ADMIN CANCELS EDITING OF USER INFO -----------------------------------------		
	$("#cancel_editUser").click(function(){
		$("#form_editUser").slideUp();
		
		return false;
	});
	
	// -------------------------------------------- ADMIN EDITS USER INFORMATION -----------------------------------------			
	$("#editUser").live('click',function(){
		var userid = $("#inp_userid").val();
		var username = $("#inp_username").val();
		var password = $("#inp_password").val();
		var confirm = $("#inp_confirm").val();
		var firstname = $("#inp_firstname").val();
		var lastname = $("#inp_lastname").val();
		var email = $("#inp_email").val();		
		var contactnumber = $("#inp_contactnumber").val();	
		var flevel = $("#inp_level").val();					
		var level;
		if (flevel=="User")
			level = 1;
		else if (flevel=="Librarian")
			level = 2;
		else
			level = 3;
		if (password != confirm)
		{
			$("#au_error_passs").slideDown(800).delay(800).slideUp();						
		}
		else if(userid != "" || username != "" || firstname != "" || lastname != "" || email != "" || contactnumber != "" || level != "")
		{
			$.post("../User/validateInformation",{
				"email": email,
				"contactnumber": contactnumber
			},function(data){
				if (data.toString()=="True")
				{					
					$.post("../User/editUser",{
						"user_ID": userid,
						"username": username,
						"password": password,
						"firstname": firstname,
						"lastname": lastname,
						"email": email,
						"contact": contactnumber,
						"level": level
					},function(data){
						if(data.toString() == "False")
						{
							$("#au_error_exists").slideDown(800).delay(800).slideUp();
						}
						else
						{
							$("#eu_success").slideDown(800).delay(800).slideUp();
							$("#form_editUser").slideUp();
							$(".error").slideUp();	
							$("#"+username+"_username").html( username );
							$("#"+username+"_firstname").html( firstname );
							$("#"+username+"_lastname").html( lastname );
							$("#"+username+"_email").val( email );	
							$("#"+username+"_contactnumber").val( contactnumber );	
							$("#"+username+"_level").html( flevel );		
						}	    
					});	
				}
				else
				{
					$("#au_error_exists").slideUp();
					$("#au_error_passs").slideUp();
					$("#au_error_emails").slideDown();					
				}			   
			});
		}			
	});
		
	// -------------------------------------------- ADMIN CANCELS EDITING OF PASSWORD -----------------------------------------		
	$("#u_cancelEditPassword").click(function(){
		$("#u_passconfirm_error").slideUp();
		$("#u_pass_error").slideUp();
		$("#u_changePassword").slideUp();
		$("#u_normalAccount").slideDown();
		
		return false;
	});
		
	// -------------------------------------------- ADMIN EDITS PASSWORD -----------------------------------------		
	$("#u_editPassword").click(function(){
		var userid = $("#userid").val();
		var newPassword = $("#u_newPassword").val();
		var confirm = $("#u_confirmNewPassword").val();
		if (newPassword != confirm)
		{
			$("#u_passconfirm_error").slideDown();
			$("#u_pass_error").slideUp();			
		}
		else
		{
			$.post("/User/adminChangePassword",{
					"user_ID" : userid,
					"password": newPassword
			},function(data){
				if(data.toString() == "False")
				{
					$("#u_pass_error").slideDown();
					$("#u_passconfirm_error").slideUp();
				}
				else
				{
					$("#u_passconfirm_error").slideUp();
					$("#u_pass_error").slideUp();
					$("#u_changePassword").slideUp();
					$("#u_newPassword").val("");	
					$("#u_confirmNewPassword").val("");	
					$("#u_pass_success").slideDown(800).delay(800).slideUp();
					$("#u_normalAccount").slideDown();
				}	    
			});
		}
		
		return false;
	});
		
	// -------------------------------------------- ADMIN WANTS TO EDIT INFO -----------------------------------------		
	$("#u_editAccount").click(function(){
		$("#u_editusername").attr("disabled", false);
		$("#u_editfirstname").attr("disabled", false);
		$("#u_editlastname").attr("disabled", false);
		$("#u_editemail").attr("disabled", false);
		$("#u_editcontact").attr("disabled", false);
		$("#u_editlevel").attr("disabled", false);
		$("#u_normalAccount").slideUp();	
		$("#u_editAccounts").slideDown();	
		
		return false;
	});
		
	// -------------------------------------------- ADMIN CANCELS EDITING OF INFO -----------------------------------------		
	$("#u_cancelEditAccount").click(function(){
		var userid = $("#userid").val();
		$.post("/User/adminGetUser",{
			"user_ID" : userid
		},function(data){							    
			$("#u_editfirstname").val(data.firstname);
			$("#u_editlastname").val(data.lastname);
			$("#u_editusername").val(data.username);
			$("#u_editemail").val(data.email);
			$("#u_editcontact").val(data.contactnumber);	
			$("#u_editusername").attr("disabled", true);
			$("#u_editfirstname").attr("disabled", true);
			$("#u_editlastname").attr("disabled", true);
			$("#u_editemail").attr("disabled", true);
			$("#u_editcontact").attr("disabled", true);
			$("#u_editlevel").attr("disabled", true);
			$("#u_normalAccount").slideDown();	
			$("#u_editAccounts").slideUp();
			
			return false;
					    
		}, "json");
	});
		
	// -------------------------------------------- ADMIN EDITS INFO -----------------------------------------		
	$("#u_editAccountFinal").click(function(){
		var userid = $("#userid").val()
		var realLevel = $("#u_editlevel").val();
		var username = $("#u_editusername").val();
		var firstname = $("#u_editfirstname").val();
		var lastname = $("#u_editlastname").val();
		var email = $("#u_editemail").val();
		var contactnumber = $("#u_editcontact").val();				
		if (realLevel=="User")
			var level = 1;
		else if (realLevel=="Librarian")
			var level = 2;
		else
			var level = 3;	
				
		if(username == '' || firstname=='' || lastname=='' || email=='' || contactnumber=='')
		{
			$("#u_accounterror").slideUp();
			$("#u_accountemail").slideUp();
			$("#u_accounterror2").slideDown();
		}
		else
		{
			$.post("/User/validateInformation",{
				"email": email
			},function(data){
				if(data.toString() == "False")
				{
					$("#u_accounterror").slideUp();
					$("#u_accounterror2").slideUp();
					$("#u_accountemail").slideDown();
				}
				else
				{	
					$.post("/User/editUser",{
					"user_ID": userid,
					"username": username,
					"password": "",
					"firstname": firstname,
					"lastname": lastname,
					"email": email,
					"contact" : contactnumber,
					"level" : level
					},function(data){
						if(data.toString() == "False")
						{
							$("#u_accounterror2").slideUp();
							$("#u_accountemail").slideUp();
							$("#u_accounterror").slideDown();
						}
						else
						{							
							$(".error").slideUp();
							$("#u_accountsuccess").slideDown(800).delay(800).slideUp();
							$("#u_editusername").attr("disabled", true);
							$("#u_editfirstname").attr("disabled", true);
							$("#u_editlastname").attr("disabled", true);
							$("#u_editemail").attr("disabled", true);
							$("#u_editcontact").attr("disabled", true);
							$("#u_editlevel").attr("disabled", true);
							$("#u_normalAccount").slideDown();	
							$("#u_editAccounts").slideUp();
			
						}						   
					});	
				}						   
			});					
		}
		
		return false;
	});
		
	// -------------------------------------------- USER CANCELS PASSWORD CHANGE-----------------------------------------		
	$("#u_changePass").click(function(){
		$("#u_normalAccount").slideUp();
		$("#u_changePassword").slideDown();
		
		return false;
	});
				
	// ------------------------------------------------- USER PROFILE --------------------------------------------------------//
				
	$(".user_column").live('click',function(){
		var user = $(this).attr("id");
		console.log(user);
		user = user.replace("_firstname","").replace("_username","").replace("_lastname","").replace("_level","");					
		window.location.replace("/User/Profile/"+user);					
	
	});			
});